<?php
/**
 * ProjectName: Grapes.
 * Created by LemonZ.
 * FileName: Role.php
 * Date: 2019/2/14
 * Time: 14:48
 * Description:
 */

namespace app\admin\controller;


use app\common\controller\Admin;
use app\common\model\AdminRole;
use app\common\model\AdminRoleOperationRel;
use think\facade\Request;


class Role extends Admin
{
    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if(Request::isAjax()){
            $adminRoleModel = new AdminRole();
            $data = input('param.');
            return $adminRoleModel->tableData($data);
        }else{
            return $this->fetch('index');
        }
    }

    /**
     * @return array|mixed
     */
    public function add()
    {
        $this->view->engine->layout(false);
        $adminRoleModel = new AdminRole();
        if(Request::isPost()){
            if(!input('?param.name')){
                return error_code(11070);
            }
            $data['name'] = input('param.name');
            $adminRoleModel->save($data);
            return [
                'status' => true,
                'data' => '',
                'msg' => '添加成功'
            ];
        }
        return $this->fetch('edit');
    }

    /**
     * @return array|mixed
     */
    public function del()
    {
        if(!input('?param.id')){
            return error_code(10000);
        }

        $adminRoleModel = new AdminRole();
        return $adminRoleModel->toDel(input('param.id'));
    }

    /**
     * @return array|mixed
     */
    public function getOperation()
    {
        $return_data = [
            'code' => 0,
            'msg'  => '获取成功',
            'data' => [],
        ];
        if (!input('?param.id')) {
            return error_code(10000);
        }
        $adminRoleModel = new AdminRole();
        $re              = $adminRoleModel->getRoleOperation(input('param.id/d'));
        if (!$re['status']) {
            return $re;
        }
        $return_data['data'] = $re['data'];
        return $return_data;
    }

    /**
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function savePerm(){
        $post = input('param.');
        if(!isset($post['id'])){
            return error_code(10000);
        }
        if(!isset($post['data'])){
            return error_code(10000);
        }
        //保存角色信息
        $adminRoleModel = new AdminRole();
        $adminRoleInfo = $adminRoleModel->where(['id'=>$post['id']])->find();
        if(!$adminRoleInfo){
            return error_code(11071);
        }
        $mrorModel = new AdminRoleOperationRel();

        $mrorModel->savePerm($post['id'],$post['data']);
        return [
            'status' => true,
            'data' => '',
            'msg' => '设置成功'
        ];
    }
}